package api

import (
	"time"

	"gitee.com/zhaokangming/tiny-ticktok/model"
	"gitee.com/zhaokangming/tiny-ticktok/pkg/app"
	"gitee.com/zhaokangming/tiny-ticktok/pkg/util/auth"
	"gitee.com/zhaokangming/tiny-ticktok/pkg/util/convert"
	"gitee.com/zhaokangming/tiny-ticktok/service"

	"github.com/gin-gonic/gin"
)

// FeedResponse get video list order by time
type FeedResponse struct {
	app.Response
	VideoList []model.Video `json:"video_list,omitempty"`
	NextTime  int64         `json:"next_time,omitempty"`
}

// Feed return video list and don't need to log in
func Feed(c *gin.Context) {
	var id int64
	claims, ok := c.Value("claims").(*auth.Claims)
	if ok {
		id = claims.ID
	}

	lastTime, ok := c.GetQuery("latest_time")
	var nextTime int64
	if ok {
		nextTime = convert.StrTo(lastTime[:10]).MustInt64()
	} else {
		nextTime = time.Now().Unix()
	}

	res, err := service.MGetVideos(c, id, nextTime)
	if err != nil {
		app.WriteResponse(c, err, nil)
		return
	}

	app.WriteResponse(c, nil, res)
}
